import React, { Component } from 'react'
import "../App.css";
import $axios from '../http';
import * as API from '../api';

export default class Login extends Component {
  state = {
    username: '',
    password: ''
  }

  login() {
    API.login(this.state).then((res) => {
      if (res.success) {
        alert(res.message);
        window.sessionStorage.setItem('token', res.token);
        this.props.history.push('/main/my');
      } else {
        alert(res.message);
      }
    })
  }
  render() {
    let user = this.state;
    return (
      <div className="LoginBox">
        <h2>登录</h2>

        <p>用户名 :<input type="text" name="username" value={user.username} onChange={(e) => {
          this.setState({ username: e.target.value })
        }} /></p>
        <p>密码 :<input type="text" name="password" value={user.password} onChange={(e) => {
          this.setState({ password: e.target.value })
        }} /></p>
        <p><button onClick={() => {
          this.login()
        }}>登录</button></p>

        <div><button onClick={() => {
          this.props.history.go(-1)
        }}>返回</button></div>
      </div>
    )
  }
}

